//
//  MyCollectionTableViewCell.m
//  DolphinMusic
//
//  Created by liuhaiyang on 15/4/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MyCollectionTableViewCell.h"

@implementation MyCollectionTableViewCell

- (void)dealloc
{
    [_button release];
    [_song release];
    [_titleLabel release];
    [_authorLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.titleLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.titleLabel];
        [_titleLabel release];
        
        self.authorLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.authorLabel];
        [_authorLabel release];
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.contentView addSubview:self.button];
        
    }
    return self;
}


- (void)buttonAction:(UIButton *)button
{
    DataBaseHandler *dbHandler = [DataBaseHandler shareInstance];
    
    [dbHandler deleteSong:self.song];
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center postNotificationName:@"songCollectionReload" object:nil];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat width = self.contentView.frame.size.width;
    CGFloat height = self.contentView.frame.size.height;
    
    self.titleLabel.frame = CGRectMake(10, 0, width - 90, height * 2 / 3);
    self.titleLabel.text = self.song.title;
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    
    self.authorLabel.frame = CGRectMake(10, height * 2 / 3, width - 90, height / 3);
    self.authorLabel.text = self.song.author;
    self.authorLabel.textColor = [UIColor lightGrayColor];
    self.authorLabel.font = [UIFont systemFontOfSize:13];
    
    self.button.frame = CGRectMake(width - 70, (height - 25) / 2, 70, 25);
    self.button.layer.cornerRadius = 5;
    self.button.layer.borderColor = [[UIColor lightGrayColor]CGColor];
    self.button.layer.borderWidth = 1;
    [self.button setTitle:@"取消收藏" forState:UIControlStateNormal];
    self.button.tintColor = [UIColor grayColor];
    self.button.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
